﻿using Kylin.Wiki.Core.CacheManager.IService;
using Kylin.Wiki.Core.Extensions;
using Kylin.Wiki.Core.Extensions.AutofacManager;
using Kylin.Wiki.Core.Utilities;
using Kylin.Wiki.IService;
using Kylin.Wiki.Services;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;

namespace Kylin.Wiki.Web.Common
{
    public class UserContext
    {
        /// <summary>
        /// 为了尽量减少redis或Memory读取,保证执行效率,将UserContext注入到DI，
        /// 每个UserContext的属性至多读取一次redis或Memory缓存从而提高查询效率
        /// </summary>
        public static UserContext Current
        {
            get
            {
                return Context.RequestServices.GetService(typeof(UserContext)) as UserContext;
            }
        }

        private static Microsoft.AspNetCore.Http.HttpContext Context
        {
            get
            {
                return HttpContext.Current;
            }
        }
        private static ICacheService CacheService
        {
            get { return GetService<ICacheService>(); }
        }

        private static T GetService<T>() where T : class
        {
            return AutofacContainerModule.GetService<T>();
        }

        private static IUserService UserService
        {
            get { return GetService<IUserService>(); }
        }

        public UserInfo UserInfo
        {
            get
            {
                if (_userInfo != null)
                {
                    return _userInfo;
                }
                return GetUserInfo(UserId);
            }
        }

        private UserInfo _userInfo { get; set; }

        public UserInfo GetUserInfo(int userId)
        {
            if (_userInfo != null) return _userInfo;
            if (userId <= 0)
            {
                _userInfo = new UserInfo();
                return _userInfo;
            }
            _userInfo = CacheService.Get<UserInfo>("kylin_" + userId.ToString());
            if (_userInfo != null && _userInfo.User_Id > 0) return _userInfo;
            _userInfo = UserService.Find(x => x.UserID == userId)
                .Select(s => new UserInfo()
                {
                    User_Id = userId, 
                    UserName = s.UserName,
                    DisplayName = s.DisplayName,
                    DeptID = s.DeptID.Value

                }).FirstOrDefault();

            if (_userInfo != null && _userInfo.User_Id > 0)
            {
                CacheService.AddObject("kylin_" + userId.ToString(), _userInfo);
            }
            return _userInfo ?? new UserInfo();
        }

        public int UserId
        {
            get
            {
                return (Context.User.FindFirstValue(JwtRegisteredClaimNames.Jti)
                    ?? Context.User.FindFirstValue(ClaimTypes.NameIdentifier)).GetInt();
            }
        }


        public string UserName
        {
            get { return UserInfo.UserName; }
        }

        public string UserTrueName
        {
            get { return UserInfo.DisplayName; }
        }

        //public string Token
        //{
        //    get { return UserInfo.Token; }
        //}

        public int Dept
        {
            get { return UserInfo.DeptID; }
        }
        public string DisplayName
        {
            get { return UserInfo.DisplayName; }
        }
         


        public void LogOut(int userId)
        {
            CacheService.Remove("kylin_" + userId.ToString());
        }
    }
}
